﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
using AutoMapper;
using System.Runtime.InteropServices;

namespace Data
{
    public class DocGiaRepository
    {
        public static List<DocGiaDTO> GetData()
        {
            List<DocGiaDTO> list = new List<DocGiaDTO>();
            try
            {
                QLTVDataContext context = new QLTVDataContext();
                var query = from dg in context.DOCGIAs
                            select dg;
                foreach (var dg in query)
                {
                    DocGiaDTO docgiaDTO = new DocGiaDTO();
                    docgiaDTO.MaDocGia = dg.MADOCGIA;
                    docgiaDTO.TenDocGia = dg.TENDOCGIA;
                    docgiaDTO.MaLoaiDocGia = dg.MALOAIDOCGIA;
                    docgiaDTO.NgaySinh = dg.NGAYSINH;
                    docgiaDTO.MaNguoiLap = dg.MANGUOILAP;
                    docgiaDTO.HanSuDung = dg.HANSUDUNG;
                    docgiaDTO.NgayLap = dg.NGAYLAP;
                    docgiaDTO.Email = dg.EMAIL;
                    docgiaDTO.DiaChi = dg.DIACHI;
                    list.Add(docgiaDTO);
                }
            }
            catch( System.Exception)
            {
                //log4net
            }
            return list;
        }
        public static void Them(DocGiaDTO docgiaDTO)
        {
            try
            {
                QLTVDataContext context = new QLTVDataContext();
                DOCGIA docgia = new DOCGIA();
                // do du lieu tu DOCGIADTO sang DOCGIA
                Mapper.CreateMap<DocGiaDTO , DOCGIA>();
                docgia = Mapper.Map<DocGiaDTO, DOCGIA>(docgiaDTO);
                context.DOCGIAs.InsertOnSubmit(docgia);
                context.SubmitChanges();
            }
            catch (System.Exception)
            {
            }
        }
        public static void Xoa(int madocgia)
        {
            try
            {
                QLTVDataContext context = new QLTVDataContext();
                var query = from p in context.DOCGIAs
                            where p.MADOCGIA == madocgia
                            select p;
                context.DOCGIAs.DeleteAllOnSubmit(query);
                context.SubmitChanges();
                
            }
            catch (System.Exception)
            {
            }
        }
        public static DocGiaDTO GetDocGia(int madocgia)
        {
            DocGiaDTO docgia = new DocGiaDTO();
            try
            {                
                QLTVDataContext context = new QLTVDataContext();
                var query = from p in context.DOCGIAs
                            where p.MADOCGIA == madocgia
                            select p;
                foreach (var dg in query)
                {
                    docgia.MaDocGia = dg.MADOCGIA;
                    docgia.TenDocGia = dg.TENDOCGIA;
                    docgia.MaLoaiDocGia = dg.MALOAIDOCGIA;
                    docgia.NgaySinh = dg.NGAYSINH;
                    docgia.MaNguoiLap = dg.MANGUOILAP;
                    docgia.HanSuDung = dg.HANSUDUNG;
                    docgia.NgayLap = dg.NGAYLAP;
                    docgia.Email = dg.EMAIL;
                    docgia.DiaChi = dg.DIACHI;
                }

            }
            catch (System.Exception)
            {
            }
            return docgia;
        }
    }
}
